import React from 'react';
import { connect } from 'umi';
import { Row, Col, Modal, message } from "antd";
import CorrelateSearch from "../../components/limit/correlate/CorrelateSearch";
import CorrelateToolbar from "../../components/limit/correlate/CorrelateToolbar";
import CorrelateList from "../../components/limit/correlate/CorrelateList";
import TablePagination from '../../components/common/TablePagination';
import LimitRelateTree from "../../components/common/LimitRelateTree";
import RelateTransfer from "../../components/common/RelateTransfer";

const CorrelatePage = (props) => {

  let { dispatch, globalModel, correlateModel } = props;
  const {
    correlateList, total, correlateLoading, operateType, currentPage, pageSize, correlateVos,
    selectedRowKeys, selectedRows, correlateInfoData, searchParams, bizType,
    relateTransferVisible, menuLimitLoading, relateList, parentRecord, correlateType, limitList
  } = correlateModel;

  const correlateSearchProps = {
    onSearch: (searchParams) => {
      dispatch({ type: "correlateModel/updateState", payload: { searchParams }});
      dispatch({ type: 'correlateModel/getCorrelatePage', payload: { currentPage, pageSize, params: searchParams }});
    },
    onReset: () => {
      dispatch({ type: "correlateModel/updateState", payload: { searchParams: null }});
    },
  };

  const correlateToolbarProps = {
    correlateType,
    onShowTransfer: () => {
      dispatch({ type: "correlateModel/getCorrelateList", payload: { type: correlateType, targetKey: parentRecord.id }}).then(res => {
        if (correlateType == "0" || correlateType == "1") {
          dispatch({ type: "correlateModel/getUserList", payload: {}});
        } else if (correlateType == "2") {
          dispatch({ type: "correlateModel/getApplicationList", payload: {}});
        } else if (correlateType == "3") {
          dispatch({ type: "correlateModel/getDepartmentList", payload: {}});
        } else if (correlateType == "4") {
          dispatch({ type: "correlateModel/getPostList", payload: {}});
        } else if (correlateType == "6") {
          dispatch({ type: "correlateModel/getRoleList", payload: { type: "relate" }});
        }
        dispatch({ type: "correlateModel/updateState", payload: { relateTransferVisible: true }});
      });
    },
    onDeleteCorrelate: () => {
      if (selectedRowKeys.length == 0) {
        message.error("请选择要删除的记录！");
        return;
      }
      Modal.confirm({
        title: "删除",
        content: "确定删除选中的记录？",
        onOk() {
          const ids = selectedRowKeys.join(",");
          dispatch({ type: "correlateModel/deleteCorrelate", payload: { type: correlateType, id: ids }});
        },
        onCancel() {
        }
      });
    }
  };

  const correlateListProps = {
    correlateType,
    currentPage,
    pageSize,
    correlateList,
    correlateLoading,
    rowSelection: {
      selectedRowKeys,
      selectedRows,
      onChange: (keys, selectedRows) => {
        dispatch({
          type: 'correlateModel/updateState',
          payload: {
            selectedRows: selectedRows,
            selectedRowKeys: keys,
          },
        })
      },
    }
  };

  const relateTransferProps = {
    correlateType,
    relateTransferVisible,
    correlateInfoData,
    relateList,
    correlateVos,
    addCorrelate: (ids) => {
      let targetCode = "0".includes(correlateType) ? parentRecord.id : parentRecord.longCode;
      let param = { type: correlateType, targetId: parentRecord.targetId, targetCode, subjectId: ids.join(",") };
      dispatch({ type: "correlateModel/addCorrelate", payload: param });
      dispatch({ type: "correlateModel/updateState", payload: { relateTransferVisible: false }});
    },
    onCancel: () => {
      dispatch({type: "correlateModel/updateState", payload: {relateTransferVisible: false}});
    },
  };

  const tablePaginationProps = {
    total,
    currentPage,
    pageSize,
    onPageChange: (currentPage, pageSize) => {
      dispatch({ type: 'correlateModel/getCorrelatePage', payload: { currentPage, pageSize, ...searchParams }});
    },
    onShowSizeChange: (currentPage, pageSize) => {
      dispatch({ type: 'correlateModel/getCorrelatePage', payload: { currentPage, pageSize, ...searchParams }});
    },
  };

  const limitTargetTreeProps = {
    bizType,
    correlateType,
    limitList,
    onRadioChange: (e) => {
      const value = e.target.value;
      dispatch({type: "correlateModel/updateState", payload: { correlateType: value }});
      dispatch({type: "correlateModel/getCorrelatePage", payload: { type: value }});

      if (value == "0") {
        dispatch({ type: "correlateModel/getAppRoleTree", payload: { type: "6" }});
      } else if (value == "1") {
        dispatch({ type: "correlateModel/getUnitNodeTree", payload: { type: "5" }});
      } else if (value == "2") {
        dispatch({ type: "correlateModel/getUnitNodeTree", payload: { type: "2" }});
      } else if (value == "3") {
        dispatch({ type: "correlateModel/getUnitNodeTree", payload: { type: "3" }});
      } else if (value == "4") {
        dispatch({ type: "correlateModel/getUnitNodeTree", payload: { type: "4" }});
      } else if (value == "6") {
        dispatch({ type: "correlateModel/getAppRoleTree", payload: { type: "2" }});
      }
    },
    onSearchCompany: (value) => {
    },
    onSelectTreeNode: (keys, event) => {
      const parentNode = event?.node;
      dispatch({ type: "correlateModel/updateState", payload: { parentRecord: parentNode }});
      dispatch({ type: 'correlateModel/getCorrelatePage', payload: { type: correlateType, targetKey: parentNode.id }});
    }
  }

  return (
    <div style={{ width: 'calc(100% - 30px)', height: 'calc(100% - 30px)', padding: "15px" }}>
      <Row style={{ height: "100%", width: "100%" }}>
        <Col span={4}>
          <LimitRelateTree { ...limitTargetTreeProps } />
        </Col>
        <Col span={20} style={{ padding: "0px 0px 0px 15px", borderLeft: "1px solid rgba(0, 0, 0, 0.06)" }}>
          <CorrelateSearch {...correlateSearchProps} />
          <CorrelateToolbar {...correlateToolbarProps} />
          <CorrelateList {...correlateListProps} />
          <TablePagination {...tablePaginationProps} />
        </Col>
      </Row>

      <RelateTransfer {...relateTransferProps}/>
    </div>
  );
};

function mapStateToProps({ globalModel, correlateModel }){
  return { globalModel, correlateModel };
}

export default connect(mapStateToProps)(CorrelatePage);
